package com.my.serviceimpl;

import com.alibaba.fastjson.JSONObject;
import com.my.bean.*;
import com.my.mapper.StockRemovalMapper;
import com.my.service.StockRemovalService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.my.service.StockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.*;

/**
 * <p>
 * 出库表 服务实现类
 * </p>
 *
 * @author 崔倩倩
 * @since 2018-08-25
 */
@Service
public class StockRemovalServiceImpl extends ServiceImpl<StockRemovalMapper, StockRemoval> implements StockRemovalService {


    @Autowired
    StockService stockService;

    @Override
    @Transactional(rollbackFor = {SQLException.class})
    public String insertStockRemoval(String param,long areaid,boolean admin) throws SQLException {
        List<Stock> stocks = new ArrayList<>();
        StockRemoval stockRemoval= JSONObject.parseObject(param,StockRemoval.class);
        stockRemoval.setDate(new Date().getTime());
       if(admin){
           Map map=new HashMap<>();
           map.put("item_name",stockRemoval.getItemName());
           map.put("areaid",areaid);
           stocks=stockService.selectByMap(map);
       }else {
           Map map=new HashMap<>();
           map.put("item_name",stockRemoval.getItemName());
           stocks=stockService.selectByMap(map);
       }
        //如果库存中存在该物品  那就只修改数量
        if (stocks.size()>0){
            Stock stock=stocks.get(0);
            double dataBaseNum=stock.getNumber();
            int requestNum=stockRemoval.getNumber();
            //如果库存大于数量  则可以出库
            if(dataBaseNum-requestNum>0){
                stockRemoval.setAreaid(areaid);
                if(this.insert(stockRemoval)){
                    stock.setNumber(dataBaseNum-requestNum);
                    if(stockService.updateById(stock)){
                        return  "出库成功";
                    }
                }
            }
            //如果库存小于数量  不可以出库
            if(dataBaseNum-requestNum<0){
                return  "无法出库，库存不足";
            }

            //如果库存等于数量  可以出  并且删除库存
            if(dataBaseNum-requestNum==0){
                stockRemoval.setAreaid(areaid);
                if(this.insert(stockRemoval)){
                    if(stockService.deleteById(stock)){
                        return  "出库成功";
                    };
                }
            }
        }else{
            return  "库存中无该物品，请检查";
        }
        throw  new SQLException();
    }

    @Override
    public ResultPage<StockRemoval> selectMyPage(VoPage voPage,StockRemoval stockRemoval,long areaid,boolean admin) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("page", (voPage.getPage() - 1)*voPage.getLimit());
        paramMap.put("limit", voPage.getLimit());
        List<StockRemoval> list = baseMapper.selectMyPage(paramMap,stockRemoval,areaid,admin);
        Integer count = baseMapper.countMyPage(paramMap,stockRemoval,areaid,admin);
        ResultPage<StockRemoval>resultPage=new ResultPage<>();
        resultPage.setCode(0);
        resultPage.setCount(count);
        resultPage.setMsg("成功");
        resultPage.setData(list);
        return resultPage;
    }
}
